package cn.dapeng.tree.binary;

import java.io.*;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.tree.binary
 * @Description TODO
 * @ClassName BinaryTree
 * @Author xupenghong
 * @Date 2024/8/20 11:44
 * @Version 1.0.0
 */
public class TreeNode implements Cloneable, Serializable {

    public Integer val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(Integer val) {
        this.val = val;
    }

    /**
     * 正儿八经的深克隆
     *
     * @return
     */
    @Override
    protected TreeNode clone() throws CloneNotSupportedException {
        super.clone();
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
        ) {
            oos.writeObject(this);
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
            return (TreeNode) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
